package com.example.camerademo.listview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.camerademo.MainActivity;
import com.example.camerademo.R;

/**
 * author: wj
 * created on: 2021/4/2 9:33
 * description:
 */
public class ListViewDemoActivity extends Activity {

    private ListView listView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_demo);

        listView = findViewById(R.id.listView);

        // 设置一个适配器，最重要的一个方法
        listView.setAdapter(new MyListAdapter(ListViewDemoActivity.this));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 设置一个弹窗，显示那个被点击了
                Toast.makeText(ListViewDemoActivity.this, "点击位置：" + position, Toast.LENGTH_SHORT).show();
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // 设置一个弹窗，显示那个被点击了
                Toast.makeText(ListViewDemoActivity.this, "长按位置：" + position, Toast.LENGTH_SHORT).show();
                if (position == 0) {
                    // 设置一个长按条目，回到MainActivity这个界面，模仿我们处理数据的跳转。
                    Intent intent = new Intent(ListViewDemoActivity.this, MainActivity.class);
                    startActivity(intent);
                }

                // 如果return true，说明处理完这个数据就可以了
                // 如果return false, 长按执行结束后，还会执行一下短按
                return true;
            }
        });
    }
}
